package xiaoqi.springboot.boot.control;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import xiaoqi.springboot.boot.service.UserService;
import xiaoqi.springboot.boot.vo.UserVO;


@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest2 {
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserService userService;

    @Test
    public void testGet2() throws Exception {
        System.out.println("before mock:" + userService.get(1));
        Mockito.when(userService.get(1)).thenReturn(
                new UserVO().setId(1).setUsername("username:1")
        );
        System.out.println("after mock:" + userService.get(1));

        ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/users/v2/1"));

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.content().json("{\n" +
                "  \"id\": 1,\n" +
                "  \"username\": \"username:1\"\n" +
                "}"));

    }
}
